import ParseData from "common.ParseData";
const mapTool = require('mapTool');

cc.Class({
    extends: cc.Component,

    properties: {
        mapNode: cc.Node,        //地图节点
        dialogNode: cc.Node,     //对话框
        loadingNode: cc.Node,    //加载节点
        tiledMap: cc.TiledMap    //单个地图测试坐标用
    },

    onLoad() {
        let self = this;

        // !! DOTO
        cc.loader.loadRes("dialogMsg.json", function (err, object) {
            if (err) {

            } else {
                console.log("加载完毕");
                console.log("ParseData = " + ParseData().parseInfo("dialogMsg.json"));
                // self.dialog = self.dialogNode.getComponent('dialog');
                // self.dialog.init(ParseData().parseInfo("dialogMsg.json"));
            }
        });

        let p = cc.director.getPhysicsManager();
        p.enabled = true;
        p.debugDrawFlags = false;
        p.gravity = cc.v2(0, 0);

        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;
        this.loadingNode.active = true;

        //存放地图数组
        let mapNameArr = [
            ['00000', '01000', '00000'],
            ['00010', '11110', '00100'],
            ['00000', '10000', '00000'],
        ];

        // let mapNameArr = mapTool.getRandNameArr();
        this.initMap(mapNameArr);
        // console.log(mapNameArr);

        //!! TODO 测试单个地图用的
        // this.initMapNode(this.tiledMap);
    },

    //根据地图名字生成地图
    initMap(mapNameArr) {
        let mapSt = null;
        let loadCount = 0;

        for (let i = 0; i < mapNameArr.length; i++) {
            for (let j = 0; j < mapNameArr[i].length; j++) {
                let mapName = mapNameArr[i][j];
                if (!mapName || mapName === '00000') continue;

                if (!mapSt) {
                    mapSt = { i, j };
                }

                loadCount++;

                //加载地图
                cc.loader.loadRes(`map/${mapName}`, cc.TiledMapAsset, (err, assets) => {
                    if (err) {
                        console.log("error = " + err);
                    } else {
                        let node = new cc.Node();
                        let map = node.addComponent(cc.TiledMap);
                        node.anchorX = node.anchorY = 0;
                        node.x = ((j - mapSt.j) * 384);
                        node.y = (-(i - mapSt.i) * 384);

                        map.tmxAsset = assets;
                        node.parent = this.mapNode;
                        this.initMapNode(map);

                        if (--loadCount == 0) {
                            this.loadingNode.active = false;
                        }
                    }
                });
            }
        }
    },

    //绘制地图
    initMapNode(mapNode) {
        // for (let mapNode of this.mapNode.children) {
        // let tileMap = mapNode.getComponent(cc.TiledMap);    //获取tiledmap地图

        let tiledSize = mapNode.getTileSize();              //获取地图尺寸
        let wallLayer = mapNode.getLayer('wall');           //获取墙的layer
        let wallSize = wallLayer.getLayerSize();            //获取墙的Size

        //战争迷雾
        // let smogLayer = mapNode.getLayer('smog');
        // smogLayer.node.active = true;

        for (let i = 0; i < wallSize.width; i++) {
            for (let j = 0; j < wallSize.height; j++) {
                let tiled = wallLayer.getTiledTileAt(i, j, true);
                if (tiled.gid != 0) {
                    tiled.node.group = 'wall';

                    let body = tiled.node.addComponent(cc.RigidBody);
                    body.type = cc.RigidBodyType.Static; //刚体设置静态
                    let collider = tiled.node.addComponent(cc.PhysicsBoxCollider);
                    collider.offset = cc.v2(tiledSize.width / 2, tiledSize.height / 2);
                    collider.size = tiledSize;
                    collider.apply();
                }

                // tiled = smogLayer.getTiledTileAt(i, j, true);
                // if (tiled.gid != 0) {
                //     tiled.node.group = 'smog';
                //     let collider = tiled.node.addComponent(cc.BoxCollider);
                //     collider.offset = cc.v2(tiledSize.width / 2, tiledSize.height / 2);
                //     collider.size = tiledSize;
                // }
            }
        }
        // }
    },

    start() {
        this.dialog = this.dialogNode.getComponent('dialog');

        // this.dialog.init([
        //     { role: 2, content: 'hello你醒了, 朗秋兄弟~!' },
        //     { role: 1, content: '我是朗秋,你是哪一位' },
        //     { role: 2, content: '朗秋,来打败我把哈哈哈哈哈~~~~!!!!!' },
        //     { role: 2, content: '哈哈哈哈哈~~~哈哈哈哈哈~~~哈哈哈哈哈~~~' },
        //     { role: 2, content: '哈哈哈哈哈~~~哈哈哈哈哈~~~哈哈哈哈哈~~~' },
        //     { role: 1, content: '大佬你找我有啥事,害怕三连,害怕三连,害怕三连!!' },
        //     { role: 2, content: '没事没事没事,测试工程罢了' },
        // ]);
    },

    // update (dt) {},
});
